﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using System.Windows.Threading;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCommon.Prompt;


namespace Baosight.ZNH.ZMCB.UI.PopWindows
{
    /// <summary>
    /// 采面信息管理
    /// 新增、修改、查看页面
    /// 对应后台ServiceZMCB04.java
    /// @author lixiangwu
    /// </summary>
    public partial class WorkingFace: EFViewCmes
    {

        public WorkingFace(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“关闭”时执行此方法，重新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    this.fdStatus.Value = "设计";
                    this.fdIsFirst.Value = "否";
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    this.fdParentId.IsEnabled = false;
                    break;
                case "detail":
                    this.Commit.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.spWindows.GetChildObjects<EFDataField>(null, true));
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            //TempButton = sender as Button;
            //TempButton.IsEnabled = false;

            //if (BeforeAction != null)
            //{
            //    BeforeAction.Invoke(null, null);
            //}
            //bool _validateField = ThisValidateField() & this.ValidateField();//确定前的效验
            //if (!_validateField)
            //{
            //    this.ValidateMessagePopup();//弹出提示信息
            //    this.TempButtonOpen(true);//确定按钮生效
            //    return;
            //}
            base.OnCommitButtonClick("ZMCB04", sender, e);
        }

        /// <summary>
        /// 提示错误消息
        /// </summary>
        /// <param name="str"></param>
        private void ValidateMessagePopup(string str)
        {
            Prompts pop = Prompts.getInstance();
            pop.SetMsg(str);
            pop.Show(6);

            return;
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        /// <summary>
        /// 采高
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdHeight_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            GetMineData();
        }

        /// <summary>
        /// 平均长度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdLength_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            GetMineData();
            this.fdSurplusToLength.Value = fdLength.Value.ToString();
        }
        /// <summary>
        /// 宽度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdWidth_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            GetMineData();
        }

        /// <summary>
        /// 容重
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdDensity_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            GetMineData();
        }
        /// <summary>
        /// 回采率
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdChance_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            GetMineData();
        }
        /// <summary>
        /// 剩余走向
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdSurplusToLength_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            GetMineData();
        }

        /// <summary>
        /// 计算储量=平均长度*采高*宽度*容重*回采率/100
        /// 计算剩余长度=平均长度-0
        /// 计算剩余储量=剩余长度*采高*宽度*容重*回采率/100
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param
        private void GetMineData()
        {
            double height = 0.0, length = 0.0, width = 0.0, density = 0.0, chance = 0.0, surplusToLength = 0.0;
            if (fdHeight.Value != null)
            {
                Double.TryParse(fdHeight.Value.ToString(), out height);
            }
            if (fdLength.Value != null)
            {
                Double.TryParse(fdLength.Value.ToString(), out length);
            }
            if (fdWidth.Value != null)
            {
                Double.TryParse(fdWidth.Value.ToString(), out width);
            }
            if (fdDensity.Value != null)
            {
                Double.TryParse(fdDensity.Value.ToString(), out density);
            }
            if (fdChance.Value != null)
            {
                Double.TryParse(fdChance.Value.ToString(), out chance);
            }
            if (fdSurplusToLength.Value != null)
            {
                Double.TryParse(fdSurplusToLength.Value.ToString(), out surplusToLength);
            }
            this.fdReserves.Value = Math.Round(height * length * width * density * chance/100, 4);
            
            this.fdSurplusReserves.Value = Math.Round(height * surplusToLength * width * density * chance/100, 4);
        }

        /// <summary>
        /// 选择所属煤层，带出容重
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdCoalseamId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFComboBox selectedData = (sender as EFDataField).Content as EFComboBox;
            EIRow selectedRow = (selectedData.SelectedItem as EIEntity).GetEIRow();
            this.fdDensity.Value = selectedRow["fdDensity"].ToString();
        }

        /// <summary>
        /// 相邻采面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdMainId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFDataField field = this.fdMain as EFDataField;
            CmesCustomView picker = field.Content as CmesCustomView;

            if (picker.ItemObject != null)
            {
                if (!picker.ItemObject.Equals(""))
                {
                    int count = (picker.ItemObject as EIInfo).GetBlock("VALUES").GetRowCount();
                    this.fdMainId.Value = (picker.ItemObject as EIInfo).GetBlock("VALUES").GetRow(0)["fdNeighborhoodId"];
                    this.fdMain.Value = (picker.ItemObject as EIInfo).GetBlock("VALUES").GetRow(0)["fdNeighborhood"];
                }
                //没选中时赋予空字符串
                else
                {
                    this.fdMainId.Value = "";
                    this.fdMain.Value = "";
                }
            }
        }
    }
}

